package com.sugar.real.estate.module.business.controller.admin.readyhouse;

import cn.hutool.core.collection.CollUtil;
import com.sugar.real.estate.framework.apilog.core.annotation.ApiAccessLog;
import com.sugar.real.estate.framework.common.pojo.CommonResult;
import com.sugar.real.estate.framework.common.pojo.PageParam;
import com.sugar.real.estate.framework.common.pojo.PageResult;
import com.sugar.real.estate.module.business.controller.admin.apartment.vo.ApartmentUpdateReqVO;
import com.sugar.real.estate.module.business.controller.admin.dicttype.vo.BizDictDataSimpleRespVO;
import com.sugar.real.estate.module.business.controller.admin.readyhouse.vo.ReadyHousePageReqVO;
import com.sugar.real.estate.module.business.controller.admin.readyhouse.vo.ReadyHouseRespVO;
import com.sugar.real.estate.module.business.controller.admin.readyhouse.vo.ReadyHouseSaveReqVO;
import com.sugar.real.estate.module.business.service.dicttype.BizDictTypeService;
import com.sugar.real.estate.module.business.service.readyhouse.ReadyHouseService;
import com.sugar.real.estate.module.business.util.SelfExcelUtil;
import com.sugar.real.estate.module.infra.controller.admin.file.vo.file.FileUploadReqVO;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import static com.sugar.real.estate.framework.apilog.core.enums.OperateTypeEnum.EXPORT;
import static com.sugar.real.estate.framework.common.pojo.CommonResult.success;

@Tag(name = "管理后台 - 现房")
@RestController
@RequestMapping("/business/ready-house")
@Validated
public class ReadyHouseController {

    @Resource
    private ReadyHouseService readyHouseService;
    @Resource
    private BizDictTypeService bizDictTypeService;

    @PostMapping("/create")
    @Operation(summary = "创建现房")
    @PreAuthorize("@ss.hasPermission('business:ready-house:create')")
    public CommonResult<Long> createReadyHouse(@Valid @RequestBody ReadyHouseSaveReqVO createReqVO) {
        return success(readyHouseService.createReadyHouse(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新现房")
    @PreAuthorize("@ss.hasPermission('business:ready-house:update')")
    public CommonResult<Boolean> updateReadyHouse(@Valid @RequestBody ReadyHouseSaveReqVO updateReqVO) {
        readyHouseService.updateReadyHouse(updateReqVO);
        return success(true);
    }

    @PutMapping("/update/status")
    @Operation(summary = "更新现房")
    @PreAuthorize("@ss.hasPermission('business:ready-house:update')")
    public CommonResult<Boolean> updateReadyHouseStatus(@Valid @RequestBody ApartmentUpdateReqVO updateReqVO) {
        readyHouseService.updateReadyHouseStatus(updateReqVO.getId(), updateReqVO.getIsShow());
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除现房")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('business:ready-house:delete')")
    public CommonResult<Boolean> deleteReadyHouse(@RequestParam("id") Long id) {
        readyHouseService.deleteReadyHouse(id);
        return success(true);
    }

    @DeleteMapping("/delete-list")
    @Parameter(name = "ids", description = "编号", required = true)
    @Operation(summary = "批量删除现房")
                @PreAuthorize("@ss.hasPermission('business:ready-house:delete')")
    public CommonResult<Boolean> deleteReadyHouseList(@RequestParam("ids") List<Long> ids) {
        readyHouseService.deleteReadyHouseListByIds(ids);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得现房")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('business:ready-house:query')")
    public CommonResult<ReadyHouseRespVO> getReadyHouse(@RequestParam("id") Long id) {
        return success(readyHouseService.getReadyHouse(id));
    }

    @GetMapping("/page")
    @Operation(summary = "获得现房分页")
    @PreAuthorize("@ss.hasPermission('business:ready-house:query')")
    public CommonResult<PageResult<ReadyHouseRespVO>> getReadyHousePage(@Valid ReadyHousePageReqVO pageReqVO) {
        return success(readyHouseService.getReadyHousePage(pageReqVO));
    }

    @PostMapping("/import-excel")
    @Operation(summary = "导入")
    @PreAuthorize("@ss.hasPermission('business:ready-house:export')")
    public CommonResult<Boolean> importExcel(FileUploadReqVO uploadReqVO) throws IOException {
        readyHouseService.importExcel(uploadReqVO);
        return success(true);
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出现房 Excel")
    @PreAuthorize("@ss.hasPermission('business:ready-house:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportReadyHouseExcel(@Valid ReadyHousePageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<ReadyHouseRespVO> list = readyHouseService.getReadyHousePage(pageReqVO).getList();
        List<BizDictDataSimpleRespVO> type1List = bizDictTypeService.getAllDictDataByTypeId(1L);
        Map<String, List<BizDictDataSimpleRespVO>> type1Map = type1List.stream().collect(Collectors.groupingBy(BizDictDataSimpleRespVO::getValue));
        List<BizDictDataSimpleRespVO> type2List = bizDictTypeService.getAllDictDataByTypeId(2L);
        Map<String, List<BizDictDataSimpleRespVO>> type2Map = type2List.stream().collect(Collectors.groupingBy(BizDictDataSimpleRespVO::getValue));
        list.forEach(apartment -> {
            List<BizDictDataSimpleRespVO> typeListOne = type1Map.get(apartment.getProjectType());
            if (CollUtil.isNotEmpty(typeListOne))
                apartment.setHouseType(typeListOne.get(0).getLabel());
            List<BizDictDataSimpleRespVO> typeList = type2Map.get(apartment.getProjectType());
            if (CollUtil.isNotEmpty(typeList))
                apartment.setProjectType(typeList.get(0).getLabel());
        });
        // 导出 Excel
        SelfExcelUtil.write(response, "现房.xls", "数据", ReadyHouseRespVO.class, list );
    }

}